<?php
	
	include_once "inc/defines.php";
	
	session_start();
	$noFieldIsSet = true;
	foreach($_POST as $item)
		if($item != "")	
			$noFieldIsSet = false;
	if(!$noFieldIsSet) {
		$sqlQuery = "SELECT id_etud, nom, prenom, login, nom_sem, nom_form FROM ".ETUDIANT_TABLE." LEFT JOIN ".GRP_TP_TABLE." ON ".ETUDIANT_TABLE.".id_grptp=".GRP_TP_TABLE.".id_grptp LEFT JOIN ".GRP_TD_TABLE." ON ".GRP_TP_TABLE.".id_grptd=".GRP_TD_TABLE.".id_grptd LEFT JOIN ".SEMESTRE_TABLE." ON ".GRP_TD_TABLE.".id_sem=".SEMESTRE_TABLE.".id_sem LEFT JOIN ".FORM_TABLE." ON ".SEMESTRE_TABLE.".id_form=".FORM_TABLE.".id_form WHERE 1=1 "; //moche mais trop feignant pour ecrire ca mieux pour l'instant (voir la suite pour comprendre)
		if($_POST["apogee"] != "")
			$sqlQuery .= "AND apogee=\"".$_POST["apogee"]."\" ";
		if($_POST["nom"] != "")
			$sqlQuery .= "AND nom=\"".$_POST["nom"]."\" ";
		if($_POST["prenom"] != "")
			$sqlQuery .= "AND prenom=\"".$_POST["prenom"]."\" ";
		if($_POST["login"] != "")
			$sqlQuery .= "AND login=\"".$_POST["login"]."\" ";
		$sqlQuery .= ";";
	}
	if(isset($sqlQuery)) {
		$db = new PDO(SERVER.":host=".HOST.";dbname=".BASE, USER, PASS);
		$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$st = $db->prepare($sqlQuery);
		$st->execute();
		$resultats = array();
		while($ligne = $st->fetch(PDO::FETCH_ASSOC)) {
			echo $ligne["id_etud"];
			$tmp = "<a class=\"searchResult\" href=\"JavaScript:void(window.open('../fiche_etu.php?id=".$ligne["id_etud"]."', 'popUpWindow','height=453,width=418,left=0,top=0,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes'));\">";
			$tmp .= (($ligne["nom"] == "") ? "N/A" : $ligne["nom"])." ";
			$tmp .= (($ligne["prenom"] == "") ? "N/A " : $ligne["prenom"])." ";
			$tmp .= "(".$ligne["login"].")";
			$tmp .= " - ".$ligne["nom_form"];
			$tmp .= " (".$ligne["nom_sem"].")";
			$tmp .= "</a>";
			if($_SESSION["authLevel"] == DDE)
				$tmp .= "&nbsp;&rarr;&nbsp;<a class=\"searchResult\" href=\"JavaScript:void(window.open('etuviewer.php?login=".$ligne["login"]."', 'popUpWindow','height=768,width=1024,left=0,top=0,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes'));\">Voir les notes</a>";
			array_push($resultats, $tmp);
		}
		$_SESSION["searchResults"] = $resultats;
	}
	header('Location: '.$_SERVER['HTTP_REFERER'], true, 302);
	
?>
